package top.smallway.lightbandassistant_app.UI;

import static top.smallway.lightbandassistant_app.UI.MainActivity.loginManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import top.smallway.lightbandassistant_app.R;
import top.smallway.lightbandassistant_app.network.LoginManager;

/**
 * 登录页面
 * 用于用户登录和注册账号
 * 提供用户名和密码的输入，以及登录和注册功能
 */
public class Login extends AppCompatActivity {
    // 用户名输入框
    private EditText username;
    // 密码输入框
    private EditText password;
    // 登录和注册按钮
    private Button login, reg;
    // 复选框
    private CheckBox check;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initview();
        login.setEnabled(false);
        check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                login.setEnabled(isChecked);
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().isEmpty()) {
                    Toast.makeText(Login.this, "请输入用户名！", Toast.LENGTH_SHORT).show();
                } else if (password.getText().toString().isEmpty()) {
                    Toast.makeText(Login.this, "请输入密码！", Toast.LENGTH_SHORT).show();
                } else {
                    String user = username.getText().toString();
                    String pass = password.getText().toString();
                    BasePopupView xPopup= new XPopup.Builder(Login.this).asLoading("正在登陆").show();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("开始登陆", "run: ");
                            loginManager.login(user, pass, new LoginManager.LoginStatus() {
                                @Override
                                public void Logined(String username, String id, String email) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Log.d("登录成功", username + "\n" + id + "\n" + email);
                                            Mine.user_name.setText(username);
                                            Mine.text_id.setText("ID:" + id);
                                            xPopup.smartDismiss();
                                            Toast.makeText(Login.this, "登陆成功", Toast.LENGTH_SHORT).show();

                                        }
                                    });
                                    finish();
                                }

                                @Override
                                public void Unlogined() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Mine.user_name.setText("登录/注册");
                                            Mine.text_id.setText("登录以享受更多服务");
                                            xPopup.smartDismiss();
                                            Toast.makeText(Login.this, "登陆失败，请检查用户名或密码", Toast.LENGTH_SHORT).show();
                                            Log.d("登录信息", "登录失败");
                                        }
                                    });
                                }
                            });
                        }
                    }).start();

                }
            }
        });

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, RegisterActivity.class));
            }
        });

    }

    private void initview() {
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        reg = findViewById(R.id.reg);
        check = findViewById(R.id.check);
    }
}
